*------------------------------------------------------------------------------
* Table 1. relationship type across times
*==============================================================================

* adjust multiple indicators (thinking of relatives as also a friend)
forvalues i = 1/5{
	gen a`i'_onlyfriend = a`i'_friend	
	replace a`i'_onlyfriend = 0 if a`i'_spouse == 1
	replace a`i'_onlyfriend = 0 if a`i'_parent == 1
	replace a`i'_onlyfriend = 0 if a`i'_child == 1
	replace a`i'_onlyfriend = 0 if a`i'_sibling == 1
	replace a`i'_onlyfriend = 0 if a`i'_othfam == 1
	replace a`i'_onlyfriend = 0 if a`i'_relative ==1

	gen a`i'_onlycowork = a`i'_cowork 
	replace a`i'_onlycowork = 0 if a`i'_spouse == 1
	replace a`i'_onlycowork = 0 if a`i'_parent == 1
	replace a`i'_onlycowork = 0 if a`i'_child == 1
	replace a`i'_onlycowork = 0 if a`i'_sibling == 1
	replace a`i'_onlycowork = 0 if a`i'_othfam == 1
	replace a`i'_onlycowork = 0 if a`i'_relative ==1

	gen a`i'_onlyneighbor = a`i'_neighbor 
	replace a`i'_onlyneighbor = 0 if a`i'_spouse == 1
	replace a`i'_onlyneighbor = 0 if a`i'_parent == 1
	replace a`i'_onlyneighbor = 0 if a`i'_child == 1
	replace a`i'_onlyneighbor = 0 if a`i'_sibling == 1
	replace a`i'_onlyneighbor = 0 if a`i'_othfam == 1
	replace a`i'_onlyneighbor = 0 if a`i'_relative ==1

	gen a`i'_onlyother = a`i'_other
	replace a`i'_onlyother = 0 if a`i'_spouse == 1
	replace a`i'_onlyother = 0 if a`i'_parent == 1
	replace a`i'_onlyother = 0 if a`i'_child == 1
	replace a`i'_onlyother = 0 if a`i'_sibling == 1
	replace a`i'_onlyother = 0 if a`i'_othfam == 1
	replace a`i'_onlyother = 0 if a`i'_relative ==1
}

* consider advisor and member into other category 
forvalues i = 1/5{
	replace a`i'_other = 1 if a`i'_advisor == 1
	replace a`i'_other = 1 if a`i'_member == 1
}

* number of spouses or relatives into an indicator having spouse or relative in networks
egen mm_spouse = rowtotal(a?_spouse)
egen mm_parent = rowtotal(a?_parent)
egen mm_sibling = rowtotal(a?_sibling)
egen mm_child = rowtotal(a?_child)
egen mm_othfam = rowtotal(a?_othfam)

egen mm_friend = rowtotal(a?_friend)
egen mm_neighbor = rowtotal(a?_neighbor)
egen mm_cowork = rowtotal(a?_cowork)
egen mm_other = rowtotal(a?_other)

egen mm_onlyfriend = rowtotal(a?_onlyfriend)
egen mm_onlyneighbor = rowtotal(a?_onlyneighbor)
egen mm_onlycowork = rowtotal(a?_onlycowork)
egen mm_onlyother = rowtotal(a?_onlyother)

for var mm_* : replace X = . if n_size == 0 | missing(n_size)
for var mm_* : replace X = 1 if X >= 1 & ~missing(X)

* relationship variables 
global rel_type_family mm_spouse mm_parent mm_sibling mm_child mm_othfam 
global rel_type_friend mm_friend mm_neighbor mm_cowork mm_other 
global rel_type_friend_only mm_onlyfriend mm_onlyneighbor mm_onlycowork mm_onlyother

estpost tabstat ${rel_type_family} ${rel_type_friend} ${rel_type_friend_only} /*
*/ [aw=wt], by(ayear) stat(mean) , if imp_name == 1
esttab using "./tables/table_reltype_multiple.csv", replace cells("${rel_type_family} ${rel_type_friend} ${rel_type_friend_only}")

* only consider the first relationship type 
global rel_type_family a1_spouse a1_parent a1_sibling a1_child a1_othfam 
global rel_type_friend a1_friend a1_neighbor a1_cowork a1_other 
global rel_type_friend_only a1_onlyfriend a1_onlyneighbor a1_onlycowork a1_onlyother

estpost tabstat ${rel_type_family} ${rel_type_friend} ${rel_type_friend_only} /*
*/ [aw=wt], by(ayear) stat(mean), if imp_name == 1
esttab using "./tables/table_reltype_first_imp.csv", replace cells("${rel_type_family} ${rel_type_friend} ${rel_type_friend_only}")

* for political discussion networks 
estpost tabstat a1_spouse a1_family a1_relative a1_neighbor a1_cowork /*
*/ [aw=wt], by(ayear) stat(mean) , if imp_name == 0
esttab using "./tables/table_reltype_first_pol.csv", replace cells("a1_spouse a1_family a1_relative a1_neighbor a1_cowork ")



